package view;

import interfaces.ViewPanel;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;

import basic.Date;

import com.toedter.calendar.JDateChooser;

import control.CategoryManager;
import control.ExpenseManager;
import control.IncomeManager;
import control.LoanManager;
import control.MemberManager;
import control.PaymentMethodManager;
import control.Search;
import control.Shell;
import control.ValidateInput;
import domain.Category;
import domain.Expense;
import domain.FamilyMember;
import domain.PaymentMethod;
import enums.LoanState;

//@author A0097788Y
public class ViewBudgetPanel extends BackgroundPanel implements ViewPanel  {

	private JTable reportTable;
	private JScrollPane reportScrollPane;

	private Vector<Vector> reportRowData; 
	private Vector<String> reportColumnNames;
	private Vector<String> expenseColumnNames;
	private Vector<String> incomeColumnNames;
	private Vector<String> loanColumnNames;

	private JLabel lblSearch;
	private JComboBox comboBoxSearchTransactions;
	private JComboBox comboBoxSearchExpenses;
	private JComboBox comboBoxSearchIncome;
	private JComboBox comboBoxSearchLoans;
	private JComboBox comboBoxSearchAll;	

	private JTextField textFieldSearch;
	private JComboBox comboBoxSearchMember;
	private JComboBox comboBoxSearchAmount;
	private JTextField textFieldSearchAmount;
	private JDateChooser calendarSearchDateFrom;
	private JLabel lblSearchDateTo;
	private JDateChooser calendarSearchDateTo;

	private JComboBox comboBoxSearchExpensesCategory;
	private JComboBox comboBoxSearchExpensesPayment;
	private JComboBox comboBoxSearchLoansState;

	private JDateChooser calendarDate;
	private JComboBox cbBar1;
	private JComboBox cbBar2;
	private JButton btnBar;
	private JLabel lblWeek;
	private JLabel lblViewExpenseBar;
	private JLabel lblAdvance;
	private JCheckBox ckbAdvance;
	private JLabel lblViewExpensePie;
	private JLabel lblVisualFeedback;
	private JComboBox PieComboBox;

	private Shell _shell;
	private ExpenseManager _expenseMan;	
	private IncomeManager _incomeMan;
	private LoanManager _loanMan;
	private MemberManager _memMan;
	private CategoryManager _catMan;
	private PaymentMethodManager _payMan;
	private Search _search;

	private String[] dayName= {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
	private String[] monthName= {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;

	/**
	 * @param xpnManager
	 * @param memManager
	 * @param catManager
	 * @param payManager
	 * @param shell
	 */
	public ViewBudgetPanel(final ExpenseManager xpnManager, final IncomeManager incomeManager, final LoanManager loanManager, 
			final MemberManager memManager, final CategoryManager catManager, final PaymentMethodManager payManager,  
			final Shell shell)
	{
		super();
		this.setLayout(null);
		_expenseMan = xpnManager;
		_incomeMan = incomeManager;
		_loanMan = loanManager;
		_shell = shell;
		_memMan = memManager;
		_catMan = catManager;
		_payMan = payManager;	

		reportScrollPane = new JScrollPane();
		reportScrollPane.setBounds(10, 11, 825, 277);
		this.add(reportScrollPane);

		reportColumnNames = Search.getToAllVectorColName();
		_search = new Search(_expenseMan, _incomeMan, _loanMan, _catMan, _memMan, _payMan);
		reportRowData = _search.toAllVector();
		updateTable();		

		lblSearch = new JLabel("Search");
		lblSearch.setBounds(30, 303, 148, 25);
		lblSearch.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		this.add(lblSearch);

		textFieldSearch = new JTextField();
		positionOfLastComboBox(textFieldSearch);
		this.add(textFieldSearch);
		textFieldSearch.setColumns(10);
		textFieldSearch.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
				{
					if(comboBoxSearchTransactions.getSelectedItem().equals("Expenses")){
						reportColumnNames = Search.getToExpenseVectorColName();
						if(comboBoxSearchExpenses.getSelectedItem().equals("Name")){
							reportRowData = _search.findExpenseString(textFieldSearch.getText());
							updateTable();
						}
						if(comboBoxSearchExpenses.getSelectedItem().equals("Paid To")){
							reportRowData = _search.findPaidTo(textFieldSearch.getText());
							updateTable();
						}
					}
					if(comboBoxSearchTransactions.getSelectedItem().equals("Income")){
						reportColumnNames = Search.getToIncomeVectorColName();
						if(comboBoxSearchIncome.getSelectedItem().equals("Name")){
							reportRowData = _search.findIncomeString(textFieldSearch.getText());
							updateTable();
						}
						if(comboBoxSearchIncome.getSelectedItem().equals("Payer")){
							reportRowData = _search.findPayer(textFieldSearch.getText());
							updateTable();
						}
					}
					if(comboBoxSearchTransactions.getSelectedItem().equals("Loan")){
						reportColumnNames = Search.getToLoanVectorColName();
						if(comboBoxSearchLoans.getSelectedItem().equals("Name")){
							reportRowData = _search.findLoanString(textFieldSearch.getText());
							updateTable();
						}
					}
					if(comboBoxSearchTransactions.getSelectedItem().equals("All")){
						reportColumnNames = Search.getToAllVectorColName();
						if(comboBoxSearchAll.getSelectedItem().equals("Name")){
							reportRowData = _search.findAllString(textFieldSearch.getText());
							updateTable();
						}
					}
				}
			}
		});

		comboBoxSearchExpensesCategory = new JComboBox();
		positionOfLastComboBox(comboBoxSearchExpensesCategory);
		this.add(comboBoxSearchExpensesCategory);
		for(Category cat : _catMan.getList()){
			comboBoxSearchExpensesCategory.addItem(cat.get_name());
		}

		comboBoxSearchMember = new JComboBox();
		positionOfLastComboBox(comboBoxSearchMember);
		this.add(comboBoxSearchMember);
		for(FamilyMember person : _memMan.getList()){
			comboBoxSearchMember.addItem(person.get_name());
		}

		calendarSearchDateFrom = new JDateChooser();
		calendarSearchDateFrom.setBounds(433, 360, 90, 20);
		calendarSearchDateFrom.setPreferredSize(new Dimension(90,20));
		calendarSearchDateFrom.setDateFormatString("dd/MM/yyyy");
		this.add(calendarSearchDateFrom);

		calendarSearchDateTo = new JDateChooser();
		calendarSearchDateTo.setBounds(538, 360, 90, 20);
		calendarSearchDateTo.setPreferredSize(new Dimension(90,20));
		calendarSearchDateTo.setDateFormatString("dd/MM/yyyy");
		this.add(calendarSearchDateTo);

		lblSearchDateTo = new JLabel();
		lblSearchDateTo.setBounds(525, 360, 15, 20);
		lblSearchDateTo.setText("to");
		this.add(lblSearchDateTo);

		comboBoxSearchAmount = new JComboBox();
		comboBoxSearchAmount.setBounds(433, 360, 50, 20);
		comboBoxSearchAmount.addItem(">");
		comboBoxSearchAmount.addItem(">=");
		comboBoxSearchAmount.addItem("=");
		comboBoxSearchAmount.addItem("<");
		comboBoxSearchAmount.addItem("<=");
		this.add(comboBoxSearchAmount);

		textFieldSearchAmount = new JTextField();
		textFieldSearchAmount.setBounds(493, 360, 135, 20);
		this.add(textFieldSearchAmount);
		textFieldSearchAmount.setColumns(10);
		textFieldSearchAmount.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
				{
					if (!ValidateInput.validAmount(textFieldSearchAmount.getText()))
						return;

					if(comboBoxSearchTransactions.getSelectedItem().equals("Expenses")){

						double amount = Double.parseDouble(textFieldSearchAmount.getText());
						reportColumnNames = Search.getToExpenseVectorColName();
						reportRowData = _search.findExpenseAmount(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();
					}
					if(comboBoxSearchTransactions.getSelectedItem().equals("Income")){
						double amount = Double.parseDouble(textFieldSearchAmount.getText());
						reportColumnNames = Search.getToIncomeVectorColName();
						reportRowData = _search.findIncomeAmount(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();
					}
					if(comboBoxSearchTransactions.getSelectedItem().equals("Loan")){
						if(comboBoxSearchLoans.getSelectedItem().equals("Amount")){
							double amount = Double.parseDouble(textFieldSearchAmount.getText());
							reportColumnNames = Search.getToLoanVectorColName();
							reportRowData = _search.findLoanAmount(amount,comboBoxSearchAmount.getSelectedIndex());
							updateTable();
						}
						if(comboBoxSearchLoans.getSelectedItem().equals("Monthly Payment")){
							double amount = Double.parseDouble(textFieldSearchAmount.getText());
							reportColumnNames = Search.getToLoanVectorColName();
							reportRowData = _search.findMonthlyPayment(amount,comboBoxSearchAmount.getSelectedIndex());
							updateTable();
						}
						if(comboBoxSearchLoans.getSelectedItem().equals("Months Left")){
							double amount = Double.parseDouble(textFieldSearchAmount.getText());
							reportColumnNames = Search.getToLoanVectorColName();
							reportRowData = _search.findMonthsLeft(amount,comboBoxSearchAmount.getSelectedIndex());
							updateTable();
						}
					}
					if(comboBoxSearchTransactions.getSelectedItem().equals("All")){
						double amount = Double.parseDouble(textFieldSearchAmount.getText());
						reportColumnNames = Search.getToAllVectorColName();
						reportRowData = _search.findAllAmount(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();
					}
				}
			}
		});

		comboBoxSearchExpensesPayment = new JComboBox();
		positionOfLastComboBox(comboBoxSearchExpensesPayment);
		this.add(comboBoxSearchExpensesPayment);
		for(PaymentMethod pay : _payMan.getList()){
			comboBoxSearchExpensesPayment.addItem(pay.get_name());
		}

		comboBoxSearchLoansState = new JComboBox();
		positionOfLastComboBox(comboBoxSearchLoansState);
		this.add(comboBoxSearchLoansState);
		comboBoxSearchLoansState.addItem("NOT_DEFINED");
		comboBoxSearchLoansState.addItem("BORROWED");
		comboBoxSearchLoansState.addItem("LENT");
		comboBoxSearchLoansState.addItem("INVESTMENT");
		comboBoxSearchLoansState.addItem("NA");


		comboBoxSearchExpenses = new JComboBox();
		comboBoxSearchExpenses.setBounds(323, 360, 100, 20);
		this.add(comboBoxSearchExpenses);
		comboBoxSearchExpenses.addItem("Name");
		comboBoxSearchExpenses.addItem("Category");
		comboBoxSearchExpenses.addItem("Amount");
		comboBoxSearchExpenses.addItem("Paid To");
		comboBoxSearchExpenses.addItem("Payment Method");
		comboBoxSearchExpenses.addItem("Date");
		comboBoxSearchExpenses.addItem("Member"); 
		comboBoxSearchExpenses.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				if(cb.getSelectedItem().equals("Name")){
					expensesFalse();
					textFieldSearch.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Member")){
					expensesFalse();
					comboBoxSearchMember.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Category")){
					expensesFalse();
					comboBoxSearchExpensesCategory.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Date")){
					expensesFalse();
					date(true);
				}
				if(cb.getSelectedItem().equals("Payment Method")){
					expensesFalse();
					comboBoxSearchExpensesPayment.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Paid To")){
					expensesFalse();
					textFieldSearch.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Amount")){
					expensesFalse();
					amount(true);
				}
			}
		});

		comboBoxSearchIncome = new JComboBox();
		comboBoxSearchIncome.setBounds(323, 360, 100, 20);
		this.add(comboBoxSearchIncome);
		comboBoxSearchIncome.addItem("Name");
		comboBoxSearchIncome.addItem("Amount"); 
		comboBoxSearchIncome.addItem("Payer");
		comboBoxSearchIncome.addItem("Date");
		comboBoxSearchIncome.addItem("Member");
		comboBoxSearchIncome.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				if(cb.getSelectedItem().equals("Name")){
					allFalse();
					textFieldSearch.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Member")){
					allFalse();
					comboBoxSearchMember.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Date")){
					allFalse();
					date(true);
				}
				if(cb.getSelectedItem().equals("Amount")){
					allFalse();
					amount(true);
				}
				if(cb.getSelectedItem().equals("Payer")){
					allFalse();
					textFieldSearch.setVisible(true);
				}
			}
		});

		comboBoxSearchLoans = new JComboBox();
		comboBoxSearchLoans.setBounds(323, 360, 100, 20);
		this.add(comboBoxSearchLoans);
		comboBoxSearchLoans.addItem("Name");
		comboBoxSearchLoans.addItem("Amount");
		comboBoxSearchLoans.addItem("Loan State");
		comboBoxSearchLoans.addItem("Date");
		comboBoxSearchLoans.addItem("Member"); 
		comboBoxSearchLoans.addItem("Due Date");
		comboBoxSearchLoans.addItem("Months Left");
		comboBoxSearchLoans.addItem("Monthly Payment");
		comboBoxSearchLoans.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				if(cb.getSelectedItem().equals("Name")){
					loansFalse();
					textFieldSearch.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Member")){
					loansFalse();
					comboBoxSearchMember.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Date")){
					loansFalse();
					date(true);
				}
				if(cb.getSelectedItem().equals("Amount")){
					loansFalse();
					amount(true);
				}

				if(cb.getSelectedItem().equals("Loan State")){
					loansFalse();
					comboBoxSearchLoansState.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Due Date")){
					loansFalse();
					date(true);
				}
				if(cb.getSelectedItem().equals("Months Left")){
					loansFalse();
					amount(true);
				}
				if(cb.getSelectedItem().equals("Monthly Payment")){
					loansFalse();
					amount(true);
				}
			}
		});

		comboBoxSearchAll = new JComboBox();
		comboBoxSearchAll.setBounds(323, 360, 100, 20);
		this.add(comboBoxSearchAll);
		comboBoxSearchAll.addItem("Name");
		comboBoxSearchAll.addItem("Amount");
		comboBoxSearchAll.addItem("Date");
		comboBoxSearchAll.addItem("Member");
		comboBoxSearchAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				if(cb.getSelectedItem().equals("Name")){
					allFalse();
					textFieldSearch.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Member")){
					allFalse();
					comboBoxSearchMember.setVisible(true);
				}
				if(cb.getSelectedItem().equals("Date")){
					allFalse();
					date(true);
				}
				if(cb.getSelectedItem().equals("Amount")){
					allFalse();
					amount(true);
				}
			}
		});

		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(comboBoxSearchTransactions.getSelectedItem().equals("Expenses")){
					reportColumnNames = Search.getToExpenseVectorColName();
					if(comboBoxSearchExpenses.getSelectedItem().equals("Name")){
						reportRowData = _search.findExpenseString(textFieldSearch.getText());
						updateTable();
					}
					if(comboBoxSearchExpenses.getSelectedItem().equals("Member")){
						reportRowData = _search.findExpenseByMem(_memMan.get(comboBoxSearchMember.getSelectedIndex()));
						updateTable();
					}
					if(comboBoxSearchExpenses.getSelectedItem().equals("Category")){
						reportRowData = _search.findByCategory(_catMan.get(comboBoxSearchExpensesCategory.getSelectedIndex()));
						updateTable();
					}
					if(comboBoxSearchExpenses.getSelectedItem().equals("Date")){
						if (!(ValidateInput.validMandatoryDate(calendarSearchDateFrom, "Date") && ValidateInput.validMandatoryDate(calendarSearchDateTo, "Date")))
							return;
						Date from = new Date(calendarSearchDateFrom.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateFrom.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateFrom.getCalendar().get(Calendar.YEAR));
						Date to = new Date(calendarSearchDateTo.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateTo.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateTo.getCalendar().get(Calendar.YEAR));
						reportRowData = _search.findExpenseByDate(from,to);
						updateTable();
					}
					if(comboBoxSearchExpenses.getSelectedItem().equals("Payment Method")){
						reportRowData = _search.findByPayment(_payMan.get(comboBoxSearchExpensesPayment.getSelectedIndex()));
						updateTable();
					}
					if(comboBoxSearchExpenses.getSelectedItem().equals("Paid To")){
						reportRowData = _search.findPaidTo(textFieldSearch.getText());
						updateTable();
					}
					if(comboBoxSearchExpenses.getSelectedItem().equals("Amount")){
						if (!ValidateInput.validAmount(textFieldSearchAmount.getText()))
							return;
						double amount = Double.parseDouble(textFieldSearchAmount.getText());
						reportRowData = _search.findExpenseAmount(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();
					}
					return;
				}
				if(comboBoxSearchTransactions.getSelectedItem().equals("Income")){
					reportColumnNames = Search.getToIncomeVectorColName();
					if(comboBoxSearchIncome.getSelectedItem().equals("Name")){
						reportRowData = _search.findIncomeString(textFieldSearch.getText());
						updateTable();
					}
					if(comboBoxSearchIncome.getSelectedItem().equals("Member")){
						reportRowData = _search.findIncomeByMem(_memMan.get(comboBoxSearchMember.getSelectedIndex()));
						updateTable();
					}
					if(comboBoxSearchIncome.getSelectedItem().equals("Date")){
						if (!(ValidateInput.validMandatoryDate(calendarSearchDateFrom, "Date") && ValidateInput.validMandatoryDate(calendarSearchDateTo, "Date")))
							return;
						Date from = new Date(calendarSearchDateFrom.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateFrom.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateFrom.getCalendar().get(Calendar.YEAR));
						Date to = new Date(calendarSearchDateTo.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateTo.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateTo.getCalendar().get(Calendar.YEAR));
						reportRowData = _search.findIncomeByDate(from,to);
						updateTable();
					}
					if(comboBoxSearchIncome.getSelectedItem().equals("Payer")){
						reportRowData = _search.findPayer(textFieldSearch.getText());
						updateTable();
					}
					if(comboBoxSearchIncome.getSelectedItem().equals("Amount")){
						if (!ValidateInput.validAmount(textFieldSearchAmount.getText()))
							return;
						double amount = Double.parseDouble(textFieldSearchAmount.getText());
						reportRowData = _search.findIncomeAmount(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();
					}

					return;
				}
				if(comboBoxSearchTransactions.getSelectedItem().equals("Loan")){
					reportColumnNames = Search.getToLoanVectorColName();
					if(comboBoxSearchLoans.getSelectedItem().equals("Name")){
						reportRowData = _search.findLoanString(textFieldSearch.getText());
						updateTable();
					}
					if(comboBoxSearchLoans.getSelectedItem().equals("Member")){
						reportRowData = _search.findLoanByMem(_memMan.get(comboBoxSearchMember.getSelectedIndex()));
						updateTable();
					}
					if(comboBoxSearchLoans.getSelectedItem().equals("Date")){
						if (!(ValidateInput.validMandatoryDate(calendarSearchDateFrom, "Date") && ValidateInput.validMandatoryDate(calendarSearchDateTo, "Date")))
							return;
						Date from = new Date(calendarSearchDateFrom.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateFrom.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateFrom.getCalendar().get(Calendar.YEAR));
						Date to = new Date(calendarSearchDateTo.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateTo.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateTo.getCalendar().get(Calendar.YEAR));
						reportRowData = _search.findLoanByDate(from,to);
						updateTable();
					}
					if(comboBoxSearchLoans.getSelectedItem().equals("Due Date")){
						if (!(ValidateInput.validMandatoryDate(calendarSearchDateFrom,  "Due Date") && ValidateInput.validMandatoryDate(calendarSearchDateTo,  "Due Date")))
							return;
						Date from = new Date(calendarSearchDateFrom.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateFrom.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateFrom.getCalendar().get(Calendar.YEAR));
						Date to = new Date(calendarSearchDateTo.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateTo.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateTo.getCalendar().get(Calendar.YEAR));
						reportRowData = _search.findDueDate(from,to);
						updateTable();
					}
					if(comboBoxSearchLoans.getSelectedItem().equals("Monthly Payment")){
						if (!ValidateInput.validAmount(textFieldSearchAmount.getText()))
							return;
						double amount = Double.parseDouble(textFieldSearchAmount.getText());
						reportRowData = _search.findMonthlyPayment(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();

					}
					if(comboBoxSearchLoans.getSelectedItem().equals("Months Left")){
						if (!ValidateInput.validAmount(textFieldSearchAmount.getText()))
							return;
						double amount = Double.parseDouble(textFieldSearchAmount.getText());
						reportRowData = _search.findMonthsLeft(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();

					}
					if(comboBoxSearchLoans.getSelectedItem().equals("Loan State")){
						switch (comboBoxSearchLoansState.getSelectedIndex()){
						case 0:
							reportRowData = _search.findLoanState(LoanState.NOT_DEFINED);
							updateTable();
							break;
						case 1:
							reportRowData = _search.findLoanState(LoanState.BORROWED);
							updateTable();
							break;
						case 2:
							reportRowData = _search.findLoanState(LoanState.LENT);
							updateTable();
							break;
						case 3:
							reportRowData = _search.findLoanState(LoanState.INVESTMENT);
							updateTable();
							break;
						case 4:
							reportRowData = _search.findLoanState(LoanState.NA);
							updateTable();
							break;
						default:
							reportRowData.clear();
						}
					}
					if(comboBoxSearchLoans.getSelectedItem().equals("Amount")){
						if (!ValidateInput.validAmount(textFieldSearchAmount.getText()))
							return;
						double amount = Double.parseDouble(textFieldSearchAmount.getText());
						reportRowData = _search.findLoanAmount(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();

					}
					return;
				}
				if(comboBoxSearchTransactions.getSelectedItem().equals("All")){
					reportColumnNames = Search.getToAllVectorColName();
					if(comboBoxSearchAll.getSelectedItem().equals("Name")){
						reportRowData = _search.findAllString(textFieldSearch.getText());
						updateTable();
					}
					if(comboBoxSearchAll.getSelectedItem().equals("Member")){
						reportRowData = _search.findAllByMem(_memMan.get(comboBoxSearchMember.getSelectedIndex()));
						updateTable();
					}
					if(comboBoxSearchAll.getSelectedItem().equals("Date")){
						if (!(ValidateInput.validMandatoryDate(calendarSearchDateFrom, "Date") && ValidateInput.validMandatoryDate(calendarSearchDateTo, "Date")))
							return;
						Date from = new Date(calendarSearchDateFrom.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateFrom.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateFrom.getCalendar().get(Calendar.YEAR));
						Date to = new Date(calendarSearchDateTo.getCalendar().get(Calendar.DAY_OF_MONTH),calendarSearchDateTo.getCalendar().get(Calendar.MONTH)+1,calendarSearchDateTo.getCalendar().get(Calendar.YEAR));
						reportRowData = _search.findAllByDate(from,to);
						updateTable();
					}
					if(comboBoxSearchAll.getSelectedItem().equals("Amount")){
						if (!ValidateInput.validAmount(textFieldSearchAmount.getText()))
							return;
						double amount = Double.parseDouble(textFieldSearchAmount.getText());	
						reportRowData = _search.findAllAmount(amount,comboBoxSearchAmount.getSelectedIndex());
						updateTable();

					}
				}
			}
		});
		btnSearch.setBounds(638, 359, 80, 23);
		this.add(btnSearch);

		JLabel lblSearchBy = new JLabel("Search by:");
		lblSearchBy.setBounds(192, 312, 121, 14);
		this.add(lblSearchBy);

		comboBoxSearchTransactions = new JComboBox();
		comboBoxSearchTransactions.setBounds(192, 360, 121, 20);
		this.add(comboBoxSearchTransactions);
		comboBoxSearchTransactions.addItem("Expenses");
		comboBoxSearchTransactions.addItem("Income");
		comboBoxSearchTransactions.addItem("Loan");
		comboBoxSearchTransactions.addItem("All");
		comboBoxSearchTransactions.setSelectedIndex(3);
		comboBoxSearchTransactions.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				if(cb.getSelectedItem().equals("Expenses")){
					expensesFalse();
					loansFalse();
					comboBoxSearchExpenses.setVisible(true);
					comboBoxSearchIncome.setVisible(false);
					comboBoxSearchLoans.setVisible(false);
					comboBoxSearchAll.setVisible(false);
					comboBoxSearchExpenses.setSelectedIndex(0);
					textFieldSearch.setVisible(true);
					reportColumnNames = Search.getToExpenseVectorColName();
					_search = new Search(_expenseMan, _catMan, _memMan, _payMan);
					reportRowData = _search.toExpenseVector();
					updateTable();
				}
				if(cb.getSelectedItem().equals("Income")){
					expensesFalse();
					loansFalse();
					comboBoxSearchExpenses.setVisible(false);
					comboBoxSearchIncome.setVisible(true);
					comboBoxSearchLoans.setVisible(false);
					comboBoxSearchAll.setVisible(false);
					comboBoxSearchIncome.setSelectedIndex(0);
					textFieldSearch.setVisible(true);
					reportColumnNames = Search.getToIncomeVectorColName();
					_search = new Search(_incomeMan);
					reportRowData = _search.toIncomeVector();
					updateTable();
				}
				if(cb.getSelectedItem().equals("Loan")){
					expensesFalse();
					loansFalse();
					comboBoxSearchExpenses.setVisible(false);
					comboBoxSearchIncome.setVisible(false);
					comboBoxSearchLoans.setVisible(true);
					comboBoxSearchAll.setVisible(false);
					comboBoxSearchLoans.setSelectedIndex(0);
					textFieldSearch.setVisible(true);
					reportColumnNames = Search.getToLoanVectorColName();
					_search = new Search(_loanMan,_memMan);
					reportRowData = _search.toLoanVector();
					updateTable();
				}
				if(cb.getSelectedItem().equals("All")){
					expensesFalse();
					loansFalse();
					comboBoxSearchExpenses.setVisible(false);
					comboBoxSearchIncome.setVisible(false);
					comboBoxSearchLoans.setVisible(false);
					comboBoxSearchAll.setVisible(true);
					comboBoxSearchAll.setSelectedIndex(0);
					textFieldSearch.setVisible(true);
					reportColumnNames = Search.getToAllVectorColName();
					_search = new Search(_expenseMan, _incomeMan, _loanMan, _catMan, _memMan, _payMan);
					reportRowData = _search.toAllVector();
					updateTable();
				}
			}
		});

		JLabel lblTransaction = new JLabel("Transaction Type:");
		lblTransaction.setBounds(192, 337, 121, 14);
		this.add(lblTransaction);

		expensesFalse();
		loansFalse();
		comboBoxSearchExpenses.setVisible(false);
		comboBoxSearchIncome.setVisible(false);
		comboBoxSearchLoans.setVisible(false);
		comboBoxSearchAll.setVisible(true);
		textFieldSearch.setVisible(true);

		//Visual Feedback
		lblVisualFeedback = new JLabel("Charts (Visual Feedback)");
		lblVisualFeedback.setBounds(30, 417, 259, 25);
		lblVisualFeedback.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		this.add(lblVisualFeedback);

		PieComboBox = new JComboBox();
		PieComboBox.setBounds(65, 481, 132, 23);
		PieComboBox.addItem("Member"); 
		PieComboBox.addItem("Category"); 
		PieComboBox.addItem("Payment Method");
		PieComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				if(cb.getSelectedItem().equals("Member")){
					PieChart pie = new PieChart("EzXpns Pie Chart", "Expense by Member");
					for(FamilyMember person : _memMan.getList()){
						double amount = _expenseMan.findAmountMem(person);
						if (amount<=0)
							continue;
						pie.add(person.get_name(),amount);
					}
					pie.create();
				}
				if(cb.getSelectedItem().equals("Category")){
					PieChart pie = new PieChart("EzXpns Pie Chart", "Expense by Category");
					for(Category cat : _catMan.getList()){
						double amount = _expenseMan.findAmountCat(cat);
						if (amount<=0)
							continue;
						pie.add(cat.get_name(),amount);
					}
					pie.create();
				}
				if(cb.getSelectedIndex()==2){
					PieChart pie = new PieChart("EzXpns Pie Chart", "Expense by Payment Method");
					for(PaymentMethod pay : _payMan.getList()){
						double amount = _expenseMan.findAmountPay(pay);
						if (amount<=0)
							continue;
						pie.add(pay.get_name(),amount);
					}
					pie.create();
				}
			}
		});

		lblViewExpensePie = new JLabel("View Pie Chart by:");
		lblViewExpensePie.setBounds(65, 460, 112, 16);
		this.add(lblViewExpensePie);
		this.add(PieComboBox);

		lblViewExpenseBar = new JLabel("View Bar Chart by:");
		lblViewExpenseBar.setBounds(261, 460, 112, 16);

		lblWeek = new JLabel("containing:");
		lblWeek.setBounds(545, 483, 112, 16);

		calendarDate = new JDateChooser();
		calendarDate.setBounds(615, 483, 90, 20);
		calendarDate.setPreferredSize(new Dimension(90,20));
		calendarDate.setDateFormatString("dd/MM/yyyy");

		lblAdvance = new JLabel("Full Period Bar Graph");
		lblAdvance.setBounds(593, 455, 112, 20);

		ckbAdvance = new JCheckBox();
		ckbAdvance.setBounds(569, 455, 20, 20);

		cbBar1 = new JComboBox();
		cbBar1.setBounds(403, 481, 132, 23);
		cbBar1.addItem("Member"); 
		cbBar1.addItem("Category"); 
		cbBar1.addItem("Payment Method"); 

		cbBar2 = new JComboBox();
		cbBar2.setBounds(261, 481, 132, 23);
		cbBar2.addItem("Week");
		cbBar2.addItem("Month");
		cbBar2.addItem("Year");
		cbBar2.addItem("Member"); 
		cbBar2.addItem("Category");  
		cbBar2.addItem("Payment Method"); 
		cbBar2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				if(cb.getSelectedItem().equals("Week") || cb.getSelectedItem().equals("Month") || cb.getSelectedItem().equals("Year")){
					lblWeek.setVisible(true);
					calendarDate.setVisible(true);
					lblAdvance.setVisible(true);
					ckbAdvance.setVisible(true);
				}
				else {
					lblWeek.setVisible(false);
					calendarDate.setVisible(false);
					lblAdvance.setVisible(false);
					ckbAdvance.setVisible(false);
				}
			}
		});

		btnBar = new JButton("Generate");
		btnBar.setBounds(715, 481, 120, 23);

		btnBar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(cbBar2.getSelectedItem().equals("Week")){

					if (!ValidateInput.validMandatoryDate(calendarDate, "Date"))
						return;
					Calendar input = calendarDate.getCalendar();
					BarChart bar = new BarChart("EzXpns Bar Chart", "Expense by ");
					boolean advance = false;
					if (ckbAdvance.isSelected())
						advance = true;
					for(int index=0;index<7;index++){ 				//this loop is for finding the 1st day of the week
						if (input.get(Calendar.DAY_OF_WEEK)==1) //1 represents sunday in Calendar
							break;
						input.roll(Calendar.DAY_OF_YEAR, false); 		//roll back 1 day
					}
					if(cbBar1.getSelectedItem().equals("Category")){
						for (int index=0;index<7;index++){ 
							Date day = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,input.get(Calendar.YEAR));
							for(Category cat : _catMan.getList()){
								double amount = _expenseMan.findAmountCatDate(cat,day);
								if (!advance)
									if (amount<=0)
										continue;
								String dateName = day.toString()+", "+ dayName[index];
								bar.add(amount, cat.get_name(), dateName);
							}
							input.roll(Calendar.DAY_OF_YEAR, true);		//roll forward 1 day
						}
					}
					if(cbBar1.getSelectedItem().equals("Member")){
						for (int index=0;index<7;index++){ 
							Date day = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,input.get(Calendar.YEAR));
							for(FamilyMember person : _memMan.getList()){
								double amount = _expenseMan.findAmountMemDate(person,day);
								if (!advance)
									if (amount<=0)
										continue;
								String dateName = day.toString()+", "+ dayName[index];
								bar.add(amount, person.get_name(), dateName);
							}
							input.roll(Calendar.DAY_OF_YEAR, true);		//roll forward 1 day
						}
					}
					if(cbBar1.getSelectedItem().equals("Payment Method")){
						for (int index=0;index<7;index++){ 
							Date day = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,input.get(Calendar.YEAR));
							for(PaymentMethod pay : _payMan.getList()){
								double amount = _expenseMan.findAmountPayDate(pay,day);
								if (!advance)
									if (amount<=0)
										continue;
								String dateName = day.toString()+", "+ dayName[index];
								bar.add(amount, pay.get_name(), dateName);
							}
							input.roll(Calendar.DAY_OF_YEAR, true);		//roll forward 1 day
						}
					}
					bar.setTitle(bar.getTitle() + cbBar1.getSelectedItem());
					bar.create();
					return;
				}
				if(cbBar2.getSelectedItem().equals("Month")){
					if (!ValidateInput.validMandatoryDate(calendarDate, "Date"))
						return;
					Calendar input = calendarDate.getCalendar();
					BarChart bar = new BarChart("EzXpns Bar Chart", "Expense by ");
					boolean advance = false;
					if (ckbAdvance.isSelected())
						advance = true;
					input.set(input.get(Calendar.YEAR), input.get(Calendar.MONTH), 1);				//set input to first day of the year
					int year = calendarDate.getCalendar().get(Calendar.YEAR);
					int month = calendarDate.getCalendar().get(Calendar.MONTH);
					boolean contLoop = true;
					if(cbBar1.getSelectedItem().equals("Category")){
						while (contLoop){ 
							Date from = new Date(input.get(Calendar.DAY_OF_MONTH),month+1,year);
							if (!(input.get(Calendar.DAY_OF_MONTH)==1 && input.get(Calendar.DAY_OF_WEEK)==1)) {
								while (input.get(Calendar.DAY_OF_WEEK)!=7){
									input.roll(Calendar.DAY_OF_YEAR, true);
									if (input.get(Calendar.MONTH)!=month){
										contLoop=false;
										input.roll(Calendar.DAY_OF_YEAR, false);
										break;
									}
								}
							}
							Date to = new Date(input.get(Calendar.DAY_OF_MONTH),month+1,year);
							for(Category cat : _catMan.getList()){
								double amount = _expenseMan.findAmountCatDate(cat, from, to);
								if (!advance)
									if (amount<=0)
										continue;
								bar.add(amount, cat.get_name(), from.toString()+" - "+to.toString());
							}
							input.roll(Calendar.DAY_OF_YEAR, true);					//go forward 1 day
						}
					}
					if(cbBar1.getSelectedItem().equals("Member")){
						while (contLoop){ 
							Date from = new Date(input.get(Calendar.DAY_OF_MONTH),month+1,year);
							if (!(input.get(Calendar.DAY_OF_MONTH)==1 && input.get(Calendar.DAY_OF_WEEK)==1)) {
								while (input.get(Calendar.DAY_OF_WEEK)!=7){
									input.roll(Calendar.DAY_OF_YEAR, true);
									if (input.get(Calendar.MONTH)!=month){
										contLoop=false;
										input.roll(Calendar.DAY_OF_YEAR, false);
										break;
									}
								}
							}
							Date to = new Date(input.get(Calendar.DAY_OF_MONTH),month+1,year);
							for(FamilyMember person : _memMan.getList()){
								double amount = _expenseMan.findAmountMemDate(person, from, to);
								if (!advance)
									if (amount<=0)
										continue;
								bar.add(amount, person.get_name(), from.toString()+" - "+to.toString());
							}
							input.roll(Calendar.DAY_OF_YEAR, true);					//go forward 1 day
						}
					}
					if(cbBar1.getSelectedItem().equals("Payment Method")){
						while (contLoop){ 
							Date from = new Date(input.get(Calendar.DAY_OF_MONTH),month+1,year);
							if (!(input.get(Calendar.DAY_OF_MONTH)==1 && input.get(Calendar.DAY_OF_WEEK)==1)) {
								while (input.get(Calendar.DAY_OF_WEEK)!=7){
									input.roll(Calendar.DAY_OF_YEAR, true);
									if (input.get(Calendar.MONTH)!=month){
										contLoop=false;
										input.roll(Calendar.DAY_OF_YEAR, false);
										break;
									}
								}
							}
							Date to = new Date(input.get(Calendar.DAY_OF_MONTH),month+1,year);
							for(PaymentMethod pay : _payMan.getList()){
								double amount = _expenseMan.findAmountPayDate(pay, from, to);
								if (!advance)
									if (amount<=0)
										continue;
								bar.add(amount, pay.get_name(), from.toString()+" - "+to.toString());
							}
							input.roll(Calendar.DAY_OF_YEAR, true);					//go forward 1 day
						}
					}
					bar.setTitle(bar.getTitle() + cbBar1.getSelectedItem());
					bar.create();
					return;
				}
				if(cbBar2.getSelectedItem().equals("Year")){
					if (!ValidateInput.validMandatoryDate(calendarDate, "Date"))
						return;
					Calendar input = calendarDate.getCalendar();
					BarChart bar = new BarChart("EzXpns Bar Chart", "Expense by ");
					boolean advance = false;
					if (ckbAdvance.isSelected())
						advance = true;
					input.set(input.get(Calendar.YEAR), 0, 1);				//set input to first day of the year
					int year = calendarDate.getCalendar().get(Calendar.YEAR);
					if(cbBar1.getSelectedItem().equals("Category")){
						for (int index=0;index<12;index++){ 
							Date from = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,year);
							input.roll(Calendar.MONTH, true);				//go to next month
							input.roll(Calendar.DAY_OF_YEAR, false); 		//go back 1 day to find last day of this month
							Date to = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,year);
							for(Category cat : _catMan.getList()){
								double amount = _expenseMan.findAmountCatDate(cat, from, to);
								if (!advance)
									if (amount<=0)
										continue;
								bar.add(amount, cat.get_name(), monthName[index]);
							}
							input.roll(Calendar.DAY_OF_YEAR, true);			//roll forward 1 day to get 1st of next month
						}
					}
					if(cbBar1.getSelectedItem().equals("Member")){
						for (int index=0;index<12;index++){ 
							Date from = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,year);
							input.roll(Calendar.MONTH, true);				//go to next month
							input.roll(Calendar.DAY_OF_YEAR, false); 		//go back 1 day to find last day of this month
							Date to = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,year);
							for(FamilyMember person : _memMan.getList()){
								double amount = _expenseMan.findAmountMemDate(person, from, to);
								if (!advance)
									if (amount<=0)
										continue;
								bar.add(amount, person.get_name(), monthName[index]);
							}
							input.roll(Calendar.DAY_OF_YEAR, true);			//roll forward 1 day to get 1st of next month
						}
					}
					if(cbBar1.getSelectedItem().equals("Payment Method")){
						for (int index=0;index<12;index++){ 
							Date from = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,year);
							input.roll(Calendar.MONTH, true);				//go to next month
							input.roll(Calendar.DAY_OF_YEAR, false); 		//go back 1 day to find last day of this month
							Date to = new Date(input.get(Calendar.DAY_OF_MONTH),input.get(Calendar.MONTH)+1,year);
							for(PaymentMethod pay : _payMan.getList()){
								double amount = _expenseMan.findAmountPayDate(pay, from, to);
								if (!advance)
									if (amount<=0)
										continue;
								bar.add(amount, pay.get_name(), monthName[index]);
							}
							input.roll(Calendar.DAY_OF_YEAR, true);			//roll forward 1 day to get 1st of next month
						}
					}
					bar.setTitle(bar.getTitle() + cbBar1.getSelectedItem());
					bar.create();
					return;
				}
				if(cbBar2.getSelectedItem().equals("Category")){
					BarChart bar = new BarChart("EzXpns Bar Chart", "Expense by Category");
					String postfix = "";
					if (cbBar1.getSelectedItem().equals("Category")){
						for(Category cat : _catMan.getList()){
							double amount = _expenseMan.findAmountCat(cat);
							if (amount<=0)
								continue;
							bar.add(amount, cat.get_name(), "Categories");
						}
					}
					if (cbBar1.getSelectedItem().equals("Member")){
						postfix = " by Member";
						for(FamilyMember person : _memMan.getList()){
							for(Category cat : _catMan.getList()){
								double amount = _expenseMan.findAmountCatMem(cat, person);
								if (amount<=0)
									continue;
								bar.add(amount, cat.get_name(), person.get_name());
							}
						}
					}
					if (cbBar1.getSelectedItem().equals("Payment Method")){
						postfix = " by Payment Method";
						for(PaymentMethod pay : _payMan.getList()){
							for(Category cat : _catMan.getList()){
								double amount = _expenseMan.findAmountCatPay(cat, pay);
								if (amount<=0)
									continue;
								bar.add(amount, cat.get_name(), pay.get_name());
							}
						}
					}
					bar.setTitle(bar.getTitle() + postfix);
					bar.create();
					return;
				}
				if(cbBar2.getSelectedItem().equals("Member")){
					BarChart bar = new BarChart("EzXpns Bar Chart", "Expense by Member");
					String postfix = "";
					if (cbBar1.getSelectedItem().equals("Member")){
						for(FamilyMember person : _memMan.getList()){
							double amount = _expenseMan.findAmountMem(person);
							if (amount<=0)
								continue;
							bar.add(amount, person.get_name(), "Members");
						}
					}
					if (cbBar1.getSelectedItem().equals("Category")){
						postfix = " by Category";
						for(Category cat : _catMan.getList()){
							for(FamilyMember person : _memMan.getList()){
								double amount = _expenseMan.findAmountCatMem(cat, person);
								if (amount<=0)
									continue;
								bar.add(amount, person.get_name(), cat.get_name());
							}
						}
					}
					if (cbBar1.getSelectedItem().equals("Payment Method")){
						postfix = " by Payment Method";
						for(PaymentMethod pay : _payMan.getList()){
							for(FamilyMember person : _memMan.getList()){
								double amount = _expenseMan.findAmountMemPay(person, pay);
								if (amount<=0)
									continue;
								bar.add(amount, person.get_name(), pay.get_name());
							}
						}
					}
					bar.setTitle(bar.getTitle() + postfix);
					bar.create();
					return;
				}
				if(cbBar2.getSelectedItem().equals("Payment Method")){
					BarChart bar = new BarChart("EzXpns Bar Chart", "Expense by Payment Method");
					String postfix = "";
					if (cbBar1.getSelectedItem().equals("Payment Method")){
						for(PaymentMethod pay : _payMan.getList()){
							double amount = _expenseMan.findAmountPay(pay);
							if (amount<=0)
								continue;
							bar.add(amount, pay.get_name(), "Payment Methods");
						}
					}
					if (cbBar1.getSelectedItem().equals("Category")){
						postfix = " by Category";
						for(Category cat : _catMan.getList()){
							for(PaymentMethod pay : _payMan.getList()){
								double amount = _expenseMan.findAmountCatPay(cat, pay);
								if (amount<=0)
									continue;
								bar.add(amount, pay.get_name(), cat.get_name());
							}
						}
					}
					if (cbBar1.getSelectedItem().equals("Member")){
						postfix = " by Member";
						for(FamilyMember person : _memMan.getList()){
							for(PaymentMethod pay : _payMan.getList()){
								double amount = _expenseMan.findAmountMemPay(person, pay);
								if (amount<=0)
									continue;
								bar.add(amount, pay.get_name(), person.get_name());
							}
						}
					}
					bar.setTitle(bar.getTitle() + postfix);
					bar.create();
					return;
				}
			}
		});

		this.add(cbBar1);
		this.add(cbBar2);
		this.add(btnBar);
		this.add(lblViewExpenseBar);
		this.add(lblWeek);
		this.add(calendarDate);
		this.add(lblAdvance);
		this.add(ckbAdvance);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 400, 825, 14);
		add(separator);

		lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(ViewBudgetPanel.class.getResource("/images/piechart32.png")));
		lblNewLabel.setBounds(30, 450, 37, 32);
		add(lblNewLabel);

		lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon(ViewBudgetPanel.class.getResource("/images/Barchart32.png")));
		lblNewLabel_1.setBounds(226, 450, 37, 32);
		add(lblNewLabel_1);

		lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setIcon(new ImageIcon(ViewBudgetPanel.class.getResource("/images/search32.png")));
		lblNewLabel_2.setBounds(157, 303, 32, 32);
		add(lblNewLabel_2);
	}



	/**
	 * 
	 * @Precondition 
	 * @Postcondition Table is updated
	 */
	public void updateTable(){
		UneditableTable vectorTable = new UneditableTable(reportRowData, reportColumnNames);
		reportTable = new JTable(vectorTable);
		reportScrollPane.setViewportView(reportTable);
		if (reportTable.getColumnCount()>5)
		{
			reportTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			for(int i=0;i<reportTable.getColumnCount();i++)
			{
				reportTable	.getColumnModel().getColumn(i).setMinWidth(120);
			}
		}
	}

	/**
	 * Reset all visibilities when viewing "Expense" in Budget Situation to false
	 * @Precondition 
	 * @Postcondition sets all UI components related to "Expense" to be invisible
	 */
	private void expensesFalse(){
		textFieldSearch.setVisible(false);
		comboBoxSearchMember.setVisible(false);
		comboBoxSearchExpensesCategory.setVisible(false);
		comboBoxSearchExpensesPayment.setVisible(false);
		date(false);
		amount(false);
	}
	/**
	 * Reset all visibilities when viewing "Loan" in Budget Situation to false
	 * @Precondition 
	 * @Postcondition sets all UI components related to "Loan" to be invisible
	 */
	private void loansFalse(){
		comboBoxSearchLoansState.setVisible(false);
		textFieldSearch.setVisible(false);
		comboBoxSearchMember.setVisible(false);
		date(false);
		amount(false);
	}
	/**
	 * Reset all visibilities when viewing "All" in Budget Situation to false
	 * Function also used for "Income"
	 * @Precondition 
	 * @Postcondition sets all UI components related to "All"/"Income" to be invisible
	 */
	private void allFalse(){
		textFieldSearch.setVisible(false);
		comboBoxSearchMember.setVisible(false);
		date(false);
		amount(false);
	}
	/**
	 * Refers to UI components related to date
	 * @param bool
	 * @Precondition 
	 * @Postcondition sets the combo boxes and label to be visible if bool is true
	 */
	private void date(boolean bool){
		lblSearchDateTo.setVisible(bool);
		calendarSearchDateTo.setVisible(bool);
		calendarSearchDateFrom.setVisible(bool);
	}
	/**
	 * Refers to UI components related to amount
	 * @param bool
	 * @Precondition 
	 * @Postcondition sets the combo box and search box to be visible if bool is true
	 */
	private void amount(boolean bool){
		comboBoxSearchAmount.setVisible(bool);
		textFieldSearchAmount.setVisible(bool);
	}
	/**
	 * Called within this class to position combo boxes
	 * @param cb
	 * @Precondition 
	 * @Postcondition sets position of the last combo box
	 */
	private void positionOfLastComboBox(JComponent cb){
		cb.setBounds(433, 360, 195, 20);
	}

	/**
	 * 
	 * @param shell
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setShell(Shell shell){
		_shell=shell;
	}
	/**
	 * 
	 * @param xpn
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setExpense(ExpenseManager xpn){
		_expenseMan=xpn;
	}
	/**
	 * 
	 * @param inc
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setIncome(IncomeManager inc){
		_incomeMan=inc;
	}
	/**
	 * 
	 * @param loan
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setLoan(LoanManager loan){
		_loanMan=loan;
	}
	/**
	 * 
	 * @param mem
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setMember(MemberManager mem){
		_memMan=mem;
	}
	/**
	 * 
	 * @param cat
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setCategory(CategoryManager cat){
		_catMan=cat;
	}
	/**
	 * 
	 * @param pay
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setPay(PaymentMethodManager pay){
		_payMan=pay;
	}
	/**
	 * 
	 * @param search
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setSearch(Search search){
		_search=search;
	}



	/**
	 * 
	 * @Precondition 
	 * @Postcondition Sets default layout
	 */
	public void setDefault()
	{
		comboBoxSearchTransactions.setSelectedIndex(3);
		expensesFalse();
		loansFalse();
		comboBoxSearchExpenses.setVisible(false);
		comboBoxSearchIncome.setVisible(false);
		comboBoxSearchLoans.setVisible(false);
		comboBoxSearchAll.setVisible(true);
		comboBoxSearchAll.setSelectedIndex(0);
		textFieldSearch.setVisible(true);
		reportColumnNames = Search.getToAllVectorColName();
		_search = new Search(_expenseMan, _incomeMan, _loanMan, _catMan, _memMan, _payMan);
		reportRowData = _search.toAllVector();
		updateTable();
	}

	/**
	 * 
	 * @param xpnManager
	 * @param shell
	 * @param memManager
	 * @param catManager
	 * @param payManager
	 * @Precondition 
	 * @Postcondition Updated view of the panel
	 */
	public void update(final ExpenseManager xpnManager, final IncomeManager incomeManager, final LoanManager loanManager, 
			final MemberManager memManager, final CategoryManager catManager, final PaymentMethodManager payManager,  
			final Shell shell)
	{
		_expenseMan = xpnManager;
		_incomeMan = incomeManager;
		_loanMan = loanManager;
		_shell = shell;
		_memMan = memManager;
		_catMan = catManager;
		_payMan = payManager;	

		comboBoxSearchExpensesCategory = new JComboBox();
		positionOfLastComboBox(comboBoxSearchExpensesCategory);
		this.add(comboBoxSearchExpensesCategory);
		for(Category cat : _catMan.getList()){
			comboBoxSearchExpensesCategory.addItem(cat.get_name());
		}

		comboBoxSearchMember = new JComboBox();
		positionOfLastComboBox(comboBoxSearchMember);
		this.add(comboBoxSearchMember);
		for(FamilyMember person : _memMan.getList()){
			comboBoxSearchMember.addItem(person.get_name());
		}

		comboBoxSearchExpensesPayment = new JComboBox();
		positionOfLastComboBox(comboBoxSearchExpensesPayment);
		this.add(comboBoxSearchExpensesPayment);
		for(PaymentMethod pay : _payMan.getList()){
			comboBoxSearchExpensesPayment.addItem(pay.get_name());
		}
		setDefault();
	}

	/**
	 * 
	 * @Precondition 
	 * @Postcondition All managers updated
	 */
	public void updateAll() {
		updateExpense();
		updateIncome();
		updateLoan();
		updateCategory();
		updateMember();
		updatePayMethod();
	}

	/**
	 * 
	 * @Precondition 
	 * @Postcondition _expense updated
	 */
	public void updateExpense() {
		setExpense(_expenseMan);
		setDefault();
	}

	/**
	 * 
	 * @Precondition 
	 * @Postcondition _incomeMan updated
	 */
	public void updateIncome() {
		setIncome(_incomeMan);
		setDefault();
	}

	/**
	 * 
	 * @Precondition 
	 * @Postcondition _loanMan updated
	 */
	public void updateLoan() {
		setLoan(_loanMan);
		setDefault();
	}

	/**
	 * 
	 * @Precondition 
	 * @Postcondition _catMan and comboBox updated
	 */
	public void updateCategory() {
		setCategory(_catMan);
		comboBoxSearchExpensesCategory = new JComboBox();
		positionOfLastComboBox(comboBoxSearchExpensesCategory);
		this.add(comboBoxSearchExpensesCategory);
		for(Category cat : _catMan.getList()){
			comboBoxSearchExpensesCategory.addItem(cat.get_name());
		}
		setDefault();
	}

	/**
	 * 
	 * @Precondition 
	 * @Postcondition _memMan and comboBox updated
	 */
	public void updateMember() {
		setMember(_memMan);
		comboBoxSearchMember = new JComboBox();
		positionOfLastComboBox(comboBoxSearchMember);
		this.add(comboBoxSearchMember);
		for(FamilyMember person : _memMan.getList()){
			comboBoxSearchMember.addItem(person.get_name());
		}
		setDefault();
	}

	/**
	 * 
	 * @Precondition 
	 * @Postcondition _payMan and comboBox updated
	 */
	public void updatePayMethod() {
		setPay(_payMan);
		comboBoxSearchExpensesPayment = new JComboBox();
		positionOfLastComboBox(comboBoxSearchExpensesPayment);
		this.add(comboBoxSearchExpensesPayment);
		for(PaymentMethod pay : _payMan.getList()){
			comboBoxSearchExpensesPayment.addItem(pay.get_name());
		}
		setDefault();
	}
}